#ifndef __CNETWORK_H__
#define __CNETWORK_H__

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <map>
#include <sstream>

#include "CNode.h"

extern pthread_mutex_t MX_RWNet;

//---------------------------------------------------------------------------//
// CNetwork
//---------------------------------------------------------------------------//
#define radiusNode 8
extern double radiusCentre;
extern double radiusWifi;
#define NBPARAM 3

class CNetwork{
private:
  std::vector <CNode *> vnodes;
  std::vector <CNode *> centres;
  std::ofstream out;

public:
  CNetwork(){};

  //---------------------------------------------------------------------------
  // Ajout d'un centre
  void addCentre(std::string p_str_id, bool remove);
  //---------------------------------------------------------------------------
  // Récupère tous les noeuds en x, y
  void getNodesAt(double p_d_x, double p_d_y, std::vector <CNode *> *p_pN_nodesAt);
  //---------------------------------------------------------------------------
  // Récupère le premier noeud en x, y
  CNode *getFirstNodeAt(double p_d_x, double p_d_y);

  //---------------------------------------------------------------------------
  // Nombre de noeud dans le reseau
  int nbNodes(){return vnodes.size();}

  //---------------------------------------------------------------------------
  // Nombre de centres dans le reseau
  int nbCentres(){return centres.size();}

  //---------------------------------------------------------------------------
  // Sauvegarde le réseau en SVG
  void insertNode(CNode *p_N_node);
  void insertArc(double x, double y, double x1, double y1);
  void filesav(std::string filename);

  //---------------------------------------------------------------------------
  // Accesseurs
  void addNode(CNode *p_N_newNode){
	//printf(";;;%s;;;\n",p_N_newNode->getName());
	vnodes.push_back(p_N_newNode);
  }
	
	void remNode(std::string str_name){
	//printf(";;;%s;;;\n",p_N_newNode->getName());
		int i;
		if((i = getNumNodeByName(str_name)) != -1){
			std::vector< CNode * >::iterator nodeIt = vnodes.begin() + i;
			vnodes.erase(nodeIt);
		}
  }
	
  CNode *getNode(int p_i_idNode){if(p_i_idNode<vnodes.size())return vnodes[p_i_idNode];}
  
  CNode *getNodeByName(std::string p_str_name){
	int i=0;
	while(i<vnodes.size()){
		//printf("compare -%s- avec -%s-\n",p_str_name.c_str(),vnodes[i]->m_str_name.c_str());
		if(p_str_name.compare(vnodes[i]->getName())==0){
			return vnodes[i];
		}
		i++;
	}
	return NULL;
  }
	
	int getNumNodeByName(std::string p_str_name){
	int i=0;
	while(i<vnodes.size()){
		//printf("compare -%s- avec -%s-\n",p_str_name.c_str(),vnodes[i]->m_str_name.c_str());
		if(p_str_name.compare(vnodes[i]->getName())==0){
			return i;
		}
		i++;
	}
	return -1;
  }

  CNode *getCentre(int p_i_idNode){if(p_i_idNode<centres.size())return centres[p_i_idNode];}
};

#endif // __CNETWORK_H__
